//
//  WelcomeViewController.swift
//  SinaWeiBo
//
//  Created by gxiangzi on 15/9/6.
//  Copyright © 2015年 hqu. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    ///  设置约束
    private var iconButtonCons:NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        iconView.sd_setImageWithURL(UserAccountViewModel.sharedUserAccount.avatarUrl)
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 开始动画
        // 1. 计算目标的约束数值
        let h = -(UIScreen.mainScreen().bounds.height + iconButtonCons!.constant)
        // 2. 修改约束数值
        // 使用自动布局，苹果提供了一个自动布局系统，在后台维护界面元素的位置和大小
        // 一旦使用了自动布局，就不要在直接设置 frame
        // 自动布局系统，会`收集`界面上所有需要重新调正位置/大小的控件的约束，然后一次性修改
        // 如果开发中需要强行更新约束，可以直接调用 layoutIfNeeded 方法，会将当前所有的约束变化应用到控件上
        iconButtonCons?.constant = h
        
        // 3. 开始动画
        label.alpha = 0
        // 开发中需要注意：不要让重要的控件移出屏幕外侧！
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            // 如果需要更新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.label.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(GXSwitchRootViewControllerNotification, object: nil, userInfo: nil)
                })
        }

    }
    
    private func setupUI() {
        // 1.添加控件
        view.addSubview(backImage)
        view.addSubview(iconView)
        view.addSubview(label)
        
        // 2.设置布局方式
        backImage.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":backImage]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":backImage]))
        
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -200))
        
        self.iconButtonCons = view.constraints.last
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 85))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 85))
        
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
        
    }

    private lazy var backImage:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView = {
       let icon =  UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius = 45
//        icon.clipsToBounds = true
        icon.layer.masksToBounds = true
        
        return icon
    }()
    
    private lazy var label:UILabel = {
        let lab = UILabel()
        lab.text = "欢迎回来"
        lab.textColor = UIColor.darkGrayColor()
        lab.sizeToFit()
        return lab
    }()
    
}
